<?php
class ProjectCategoriesController extends AppController
{
    public $name = 'ProjectCategories';
    public function index()
    {
        $this->pageTitle = __l('Project Categories');
        $this->ProjectCategory->recursive = 0;
        $limit = Configure::read('Project.no_of_items_to_display_in_category');
		if (!empty($this->request->params['named']['type']) && $this->request->params['named']['type'] == 'home') {
        	$limit = 12;
        }
		$this->paginate = array(
            'fields' => array(
                'ProjectCategory.name',
                'ProjectCategory.slug'
            ) ,
            'limit' => $limit ,
            'recursive' => - 1,
            'order' => 'ProjectCategory.name asc',
        );
		$this->set('projectCategories', $this->paginate());
		if (!empty($this->request->params['named']['type']) && $this->request->params['named']['type'] == 'home') {
			$this->autoRender = false;
			$this->render('home');
		}
    }
    public function admin_index()
    {
        $this->pageTitle = __l('Project Categories');
        $this->ProjectCategory->recursive = 1;
        $this->set('projectCategories', $this->paginate());
    }
    public function admin_add()
    {
        $this->pageTitle = __l('Add Project Category');
        if (!empty($this->request->data)) {
            $this->ProjectCategory->create();
            if ($this->ProjectCategory->save($this->request->data)) {
                $this->Session->setFlash(__l('Project Category has been added') , 'default', null, 'success');
                $this->redirect(array(
                    'action' => 'index'
                ));
            } else {
                $this->Session->setFlash(__l('Project Category could not be added. Please, try again.') , 'default', null, 'error');
            }
        }
    }
    public function admin_edit($id = null)
    {
        $this->pageTitle = __l('Edit Project Category');
        if (is_null($id)) {
            throw new NotFoundException(__l('Invalid request'));
        }
        if (!empty($this->request->data)) {
            if ($this->ProjectCategory->save($this->request->data)) {
                $this->Session->setFlash(__l('Project Category has been updated') , 'default', null, 'success');
            } else {
                $this->Session->setFlash(__l('Project Category could not be updated. Please, try again.') , 'default', null, 'error');
            }
        } else {
            $this->request->data = $this->ProjectCategory->read(null, $id);
            if (empty($this->request->data)) {
                throw new NotFoundException(__l('Invalid request'));
            }
        }
        $this->pageTitle.= ' - ' . $this->request->data['ProjectCategory']['name'];
    }
    public function admin_delete($id = null)
    {
        if (is_null($id)) {
            throw new NotFoundException(__l('Invalid request'));
        }
        if ($this->ProjectCategory->delete($id)) {
            $this->Session->setFlash(__l('Project Category deleted') , 'default', null, 'success');
            $this->redirect(array(
                'action' => 'index'
            ));
        } else {
            throw new NotFoundException(__l('Invalid request'));
        }
    }
}
?>